﻿<!--Sub-menu-->
<?php require __DIR__ . '/../_submenu.php'; ?>
<?php //Yii::app()->clientScript->registerCoreScript('jquery'); ?>
<?php //Yii::app()->clientScript->registerScriptFile('https://www.google.com/jsapi'); ?>
<?PHP Yii::app()->clientScript->registerCssFile('css/stock/fomular.css'); ?>
<?php
if(isset($_GET['port'])) include "menu.php";

if (isset($_GET['port']))
    $tickerCount = Portfolio::model()->findAll(array('select' => 'companyID', 'distinct' => true, 'condition' => "portfolionameID = " . $_GET['port']));
else
    $tickerCount = "0";
?>
<!--`Stock` box updated due to changes on `House` box-->

<h1>Stock Formula</h1>
Number of asset: <strong><?php echo count($tickerCount); ?></strong>
<div class="form">
    <form>
        <?PHP if (count($tickerCount) == 0 || $tickerCount == "0")
            echo "Không có cổ phiếu nào trong danh mục quản lý của bạn!"; else { ?>
            <?PHP
            echo "
            <div id=\"stock-grid\" class=\"grid-view\">
                <table class=\"items\" style='margin-bottom: 2px;'>
                    <tr><td valign=\"top\">
                        <table>";
            echo "<tr><td><B>Exp</B></td><td><INPUT value=\"0.55\" Name=\"BorrowRate\" SIZE=3></td></tr>";
            echo "<tr><td><B>Depth</B></td><td><INPUT value=\"30\" Name=\"depth\" SIZE=3></td></tr>";
            echo "<tr><td><B>Step</B></td><td><INPUT value=\"1\" Name=\"step\" SIZE=3></td></tr>";
            echo "
                        </table>
                    </td>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td>";

            echo "<TABLE border=1 style='margin-bottom: 2px;'>
					  <TR> <TH Valign=\"bottom\">Asset #</TH>
						   <TH Align=\"center\">Stock";

            echo "</TH>";
            echo "</TR> ";
            $i = 1;
            foreach ($tickerCount as $abc) {

                $ticker_Obj = Company::model()->findByAttributes(array('id' => $abc->companyID));
                echo "<tr><td>$i</td><td>
                       <input type=\"text\" id=\"stock_id$i\" name=\"stock_id$i\" class=\"stock_id$i\" value=\"" . $ticker_Obj->ticket . "\" readonly=\"readonly\">
                        </td>";

                echo "</tr>";
                $i++;
            }
            echo "</table>";

            echo"</td></tr></table></div>";
            ?>

        <?PHP } ?>
        <input id="house" name="house" type="hidden" value="<?php echo count($tickerCount); ?>" >
        <?php
        echo CHtml::ajaxSubmitButton(
                'Get Data', array('ajax/FormulaCalGetData'), array(
            'class' => 'submitButton',
            'update' => '#showFormResult1',
            'error' => 'js:function(){
                    alert(\'Error\');
                }',
            'beforeSend' => 'js:function(){
                    var status = document.getElementById(\'status\');
                    status.innerHTML = "Calculating...";
                }',
            'complete' => 'js:function(){
                    var status = document.getElementById(\'status\');
                    status.innerHTML = "Done";
                }'
                )
        );
        echo CHtml::ajaxSubmitButton(
                'Calculate', array('ajax/FormulaCal'), array(
            'class' => 'submitButton',
            'update' => '#showFormResult',
            'error' => 'js:function(){
                    alert(\'Error\');
                }',
            'beforeSend' => 'js:function(){
                    var status = document.getElementById(\'status\');
                    status.innerHTML = "Calculating...";
                }',
            'complete' => 'js:function(){
                    var status = document.getElementById(\'status\');
                    status.innerHTML = "Done";
                }'
                )
        );
        ?>
        <div id="status">&nbsp;</div>


        <!--Show the form based on selected `house`-->
        <div class="showForm" id="showForm">
        </div>
        <hr>
        <div class="showFormResult" id="showFormResult">

        </div>
        <div class="showFormResult1" id="showFormResult1">

        </div>
        <?PHP if (isset($_GET['result']) && isset($_GET['asset'])) { ?>
            <script type="text/javascript">
            
                // Load the Visualization API and the piechart package.
                google.load('visualization', '1.0', {'packages':['corechart']});
              
                // Set a callback to run when the Google Visualization API is loaded.
                google.setOnLoadCallback(drawChart);
              
                // Callback that creates and populates a data table, 
                // instantiates the pie chart, passes in the data and
                // draws it.
                function drawChart() {

                    // Create the data table.
                    var data = new google.visualization.DataTable();
                    data.addColumn('string', 'Topping');
                    data.addColumn('number', 'Slices');
                    data.addRows([
    <?PHP
    if (isset($_GET['asset'])) {
        $pieces = explode(",", $_GET['asset']);
        $count = 1;
        foreach ($pieces as $temp) {
            echo "['Asset " . $count . "'," . $temp . "],";
            $count++;
        }
    }
    ?>
            ]);

            // Set chart options
            var options = {'title':'How Much Pizza I Ate Last Night',
                'width':400,
                'height':300};

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, options);
        }
        //google.load('visualization', '1.0', {'packages':['corechart']});
              
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChartResult);
              
        // Callback that creates and populates a data table, 
        // instantiates the pie chart, passes in the data and
        // draws it.
        function drawChartResult() {

            // Create the data table.
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Topping');
            data.addColumn('number', 'Slices');
            data.addRows([
    <?PHP
    if (isset($_GET['result'])) {
        $pieces = explode(",", $_GET['result']);
        $count = 1;
        foreach ($pieces as $temp) {
            echo "['Asset " . $count . "'," . $temp . "],";
            $count++;
        }
    }
    ?>
            ]);

            // Set chart options
            var options = {'title':'How Much Pizza I Ate Last Night',
                'width':400,
                'height':300};

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.PieChart(document.getElementById('chart_div_result'));
            chart.draw(data, options);
        }
            </script>
            <!--Div that will hold the pie chart-->
            <table><tr><td>
                        <div id="chart_div"></div></td><td>
                        <div id="chart_div_result"></div></td></tr></table>
        <?PHP } ?>
    </form>
</div>
<?php $this->renderPartial("report/formula/_changeMeanDialogBox"); ?>
<script type="text/javascript">
    $(".btChangeMean").live("click", function() {
        $("#sourceInput").attr("value", $(this).attr("id"));
        $("#changeMeanDlg").dialog("open");
    });
    function ApplyMeanChanges() {
        var source = $("#sourceInput").attr("value");
        source = source.replace("_", "mean");
        
        var bes_pro = $("#best_probality").attr("value");
        var nor_pro = $("#normal_probality").attr("value");
        var wor_pro = $("#worst_probality").attr("value");
        
        var bes_rtn = $("#best_return").attr("value");
        var nor_rtn = $("#normal_return").attr("value");
        var wor_rtn = $("#worst_return").attr("value");
        
        if(bes_pro != "" && wor_pro != "" && nor_pro != "" && bes_rtn != "" && wor_rtn != "" && nor_rtn != "" && !isNaN(bes_pro) && !isNaN(nor_pro) && !isNaN(wor_pro) && !isNaN(bes_rtn) && !isNaN(nor_rtn) && !isNaN(wor_rtn)) {
            var avg = (bes_pro * bes_rtn + nor_pro * nor_rtn + wor_pro * wor_rtn);
            var result = Math.sqrt(bes_pro * (bes_rtn - avg) * (bes_rtn - avg) + nor_pro * (nor_rtn - avg) * (nor_rtn - avg) + wor_pro * (wor_rtn - avg) * (wor_rtn - avg));
            //alert(result);
            $("#" + source).attr("value", result);
            $("#changeMeanDlg").dialog("close");
        }
        else {
            alert("Invalid values!");
        }
    }
    function CancelMeanChanges() {
        $("#changeMeanDlg").dialog("close");
    }
</script>
